package com.silence.whereeat.servlet;

import com.silence.whereeat.dao.FoodInfoDao;
import com.silence.whereeat.dao.StoreInfoDao;
import com.silence.whereeat.model.FoodInfo;
import com.silence.whereeat.service.FoodInfoService;
import com.silence.whereeat.service.StoreInfoService;
import com.silence.whereeat.utils.Content;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 上传食物信息
 * Created by 伟阳 on 2015/9/15.
 */
public class UploadFoodServlet extends HttpServlet {

    private FoodInfoService uploadFood;
    private StoreInfoService checkStore;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String food_name = request.getParameter("food_name").trim();
        String food_price = request.getParameter("food_price").trim();
        String food_introduction = request.getParameter("food_introduction").trim();
        int store_id = Integer.parseInt(request.getParameter("store_id").trim());
        PrintWriter out = response.getWriter();
        if (checkStore.isExist(store_id)) {
            FoodInfo foodInfo = new FoodInfo();
            foodInfo.setFood_name(food_name);
            foodInfo.setFood_price(food_price);
            foodInfo.setFood_introduction(food_introduction);
            foodInfo.setStore_id(store_id);
            int result = uploadFood.addFoodInfo(foodInfo);
            if (result == 1) {
                out.println(Content.STR_SUCCESS);
            } else {
                out.println(Content.STR_FAILED);
            }
        } else {
            out.println(Content.STR_EXIST);
        }
        out.flush();
        out.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    public void init() throws ServletException {
        uploadFood = new FoodInfoDao();
        checkStore = new StoreInfoDao();
    }
}
